package com.hydl.power.controller;

import com.github.pagehelper.PageInfo;
import com.hydl.power.aop.annotation.LogAnnotation;
import com.hydl.power.pojo.Device;
import com.hydl.power.pojo.OrderDelivery;
import com.hydl.power.service.DeviceService;
import com.hydl.power.service.FaultAnalyzeService;
import com.hydl.power.service.OrderDeliveryService;
import com.hydl.power.utils.IdUtil;
import com.hydl.power.utils.Resp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wxb
 * @date 2020/4/17 09:41
 */
@Api(value = "IncidentController",tags = "事件记录/告警列表")
@RequestMapping("/incident")
@RestController
@CrossOrigin
public class IncidentController {

    @Autowired
    private OrderDeliveryService orderDeliveryService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private FaultAnalyzeService faultAnalyzeService;

    //@LogAnnotation(module = "事件记录/告警列表",description = "告警列表")
    @ApiOperation("事件记录 告警列表")
    @PostMapping("/getIncidentRecord")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "页数", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "条数",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "beginTime", value = "开始故障时间",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "结束故障时间",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "rank", value = "处理级别  1 一般 2紧急 3严重", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "faultTypes", value = "故障类型(告警类型) 1越限告警 2变位告警 3电量反转 4时间反转 5数据异常 6 通信状态告警", dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "orderStatus", value = "工单状态  1待派送 2待处理(派送工单) 3处理中 4 忽略工单(已忽略) 5 已结束",dataType = "Integer"),
            @ApiImplicitParam(paramType = "query", name = "roomName", value = "配电房名字", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "deviceName", value = "设备名字",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "deviceAddress", value = "设备地址",dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "token", value = "token",dataType = "String"),
    })
    public Resp<PageInfo<OrderDelivery>> getIncidentRecord(Integer pageNum, Integer pageSize, String begin, String end, Integer rank, Integer faultTypes, Integer orderStatus, String roomName, String deviceName, String deviceAddress,String token){
        PageInfo<OrderDelivery> orderDeliveryPageInfo = orderDeliveryService.getIncidentRecord(pageNum, pageSize, IdUtil.str2Long(begin), IdUtil.str2Long(end), rank, faultTypes, orderStatus, roomName, deviceName, deviceAddress,token);
        return Resp.success(orderDeliveryPageInfo);
    }

    //@LogAnnotation(module = "事件记录/告警列表",description = "获取最新一条告警记录")
    @PostMapping("/getOrderDeliveryByfaultTime")
    @ApiOperation(value = "获取最新一条告警记录", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "String", dataType = "String"),
    })
    public Resp<OrderDelivery> getOrderDeliveryByfaultTime(String token) {
        OrderDelivery OrderDelivery = orderDeliveryService.getOrderDeliveryByfaultTime(token);
        return Resp.success(OrderDelivery);
    } 


    @PostMapping("/getOrderDeliveryByTotal")
    @ApiOperation(value = "获取最近几条告警记录", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "String", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "total", value = "Integer", dataType = "String"),
    })
    public Resp getOrderDelivery(String token,Integer total) {
        List<OrderDelivery> list= orderDeliveryService.getOrderDeliveryByTotal(token,total);
        return Resp.success(list);
    }

    @PostMapping("/getOrderDeliveryTotal")
    @ApiOperation(value = "获取未处理的告警数量", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "String", dataType = "String"),
    })
    public Resp getOrderDeliveryTotal(String token) {
        Integer total =orderDeliveryService.getOrderDeliveryTotal(token);
        return Resp.success(total);
    }



    // @LogAnnotation(module = "事件记录/告警列表",description = "改变告警状态")
    @PostMapping("/updateflag")
    @ApiOperation(value = "改变告警状态", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "orderDeliveryId", value = "id", required = true, dataType = "String"),
    })
    public Resp<OrderDelivery> updateflag(String orderDeliveryId) {
        int i = orderDeliveryService.updateflag(orderDeliveryId);
        if (i == 1){
            return Resp.success("修改成功");
        }else{
            return Resp.fail("修改失败");
        }
    }


    @LogAnnotation(module = "事件记录/告警列表",description = "添加事件记录")
    @ApiOperation("添加事件记录/告警列表")
    @PostMapping("/addIncidentRecordOrderDelivery")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "ids", value = "选中的id", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "fault", value = "告警内容", required = true, dataType = "String"),
    })
    public Resp addIncidentRecordOrderDelivery(String ids, String fault) {
        Device device = deviceService.selectBydeviceId(ids);
        if (device == null){
            return Resp.fail("请选择设备名称");
        }
        int i = orderDeliveryService.addIncidentRecordOrderDelivery(ids, fault);
        if (i == 1){
            return Resp.success("添加成功");
        }else{
            return Resp.fail("添加失败");
        }
    }


    @LogAnnotation(module = "事件记录/告警列表",description = "批量改变告警状态")
    @PostMapping("/batchUpdateOrderStatus")
    @ApiOperation(value = "批量改变告警状态", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "orderDeliveryIds", value = "id", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "orderStatus", value = "工单状态  1待派送 2待处理(派送工单) 3处理中 4 忽略工单(以忽略) 5 以结束", required = true, dataType = "Integer"),
    })
    public Resp batchUpdateOrderStatus(String orderDeliveryIds,Integer orderStatus) {
        int i = orderDeliveryService.batchUpdateOrderStatus(orderDeliveryIds,orderStatus);
        return Resp.success();

    }


    @LogAnnotation(module = "事件记录/告警列表",description = "一键忽略工单")
    @PostMapping("/batchUpdateOrderDelivery")
    @ApiOperation(value = "一键忽略工单", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", required = true, dataType = "String"),
    })
    public Resp batchUpdateOrderDelivery(String token) {
        int i = orderDeliveryService.batchUpdateOrderDelivery(token);
        return Resp.success();

    }



    // @LogAnnotation(module = "事件记录/告警分析",description = "告警分析")
    @PostMapping("/queryFaultAnalyze")
    @ApiOperation(value = "告警分析", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "cityId", value = "cityId", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "projectId", value = "projectId", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "roomId", value = "roomId", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "token", value = "token", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "startTime", value = "startTime", required = false, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "endTime", value = "endTime", required = false, dataType = "String")
    })
    public Resp aaa(String cityId,String projectId,String roomId,String token,String startTime,String endTime) {
        return Resp.success(faultAnalyzeService.queryFaultAnalyze(cityId,projectId,roomId,token,startTime,endTime));
    }



}
